import user from '../../../utils/user.js'
import {
    createClientQrCode,
} from '../../../api/app.js'
import {
    base64ToWxfile,
    createQrcode
} from '../../../utils/util.js'

const app = getApp()
Page({
    data: {
        bg: [{
            preview: '/packagePay/assets/img/poster/poster_bg_1.png',
            original: '/packagePay/assets/img/poster/poster_bg_1_1.png'
        },
        {
            preview: '/packagePay/assets/img/poster/poster_bg_2.png',
            original: '/packagePay/assets/img/poster/poster_bg_2_1.png'
        },
        {
            preview: '/packagePay/assets/img/poster/poster_bg_3.png',
            original: '/packagePay/assets/img/poster/poster_bg_3_1.png'
        },
        {
            preview: '/packagePay/assets/img/poster/poster_bg_4.png',
            original: '/packagePay/assets/img/poster/poster_bg_4_1.png'
        },
        {
            preview: '/packagePay/assets/img/poster/poster_bg_5.png',
            original: '/packagePay/assets/img/poster/poster_bg_5_1.png'
        }
        ],
        active: 1,
        postStatus: true,
        supplierName: '',
        supplierId: '',
        workers: [],
        showPosterStyle: false,
        record: true,
        type: ''
    },
    onReady() {
        user.waitLogin().then(() => {
            this.setData({
                name: user.userName,
                postStatus: true
            })
            this.saveQrCode()
        })

    },
    onLoad(option) {
        console.log(option, 'option')
        this.setData({
            supplierName: option.projectName.split('有限公司')[0],
            supplierNameEnglish: '',
            supplierId: option.projectId,
            isBook: !!option.book,
            record: !option.notRecord,
            active: option.poster || 1,
            type: option.type
        })
        this.createInviteQrCode()
        // 获取通过邀请码进入的列表
    },
    // onUnload(){
    //     console.log(this.data.socket)
    //     this.data.socket.close()
    // },

    async createInviteQrCode() {
        console.log(this.data.supplierId, 'this.data.supplierId')
        if (this.data.supplierId === 'null') {
            wx.showToast({
                title: '创建二维码失败,请检查项目状态',
                icon: 'none'
            })
            return
        }
        let scene = ''
        if (this.data.isBook) {
            scene = 'PCID=' + user.companyId + '&UID=' + user.userId
        } else {
            scene = 'PPID=' + this.data.supplierId + '&UID=' + user.userId + (this.data.type ? '&ADDT=' + '5' : '')
        }
        console.log(scene, 'scene')
        console.log(5555, encodeURIComponent(scene))
        const text = app.globalData.qrCodeUrl + '?qrcodeType=normal&scene=' + encodeURIComponent(scene)
        console.log(111, text)
        const qrCode = await createQrcode('qrcode', text)
        this.setData({
            qrcode: qrCode
        })
        this.saveQrCode()

        // console.log(123, qrCode)
        // createClientQrCode({
        //     page: 'pages/join/join',
        //     scene: scene,
        //     width: 280
        // }).then(res => {
        //     var qrCode = res // 后台返回的base64图片流
        //     base64ToWxfile(qrCode, Math.ceil(Math.random() * 10000000000) + '.png').then(path => {
        //         this.setData({
        //             qrcode: path
        //         })
        //         this.saveQrCode()
        //     })
        // })
    },
    selectBg(event) {
        console.log(event, 'event')
        this.setData({
            active: event.currentTarget.dataset.key,
            postStatus: false
        })
        this.setData({
            postStatus: true
        })
        wx.showLoading({
            title: '正在生成请稍后'
        })
        setTimeout(() => {
            wx.hideLoading()
            this.saveQrCode()
        }, 100)
        // this.saveQrCode()
    },
    saveQrCode() {
        const ctx = wx.createCanvasContext('myCanvas')
        ctx.save()
        ctx.rect(0, 0, 335, 500)
        ctx.setFillStyle('#fff')
        ctx.fill()
        ctx.clip() // 剪切
        // 背景放置
        ctx.drawImage(this.data.bg[this.data.active].original, 0, 0, 335, 455, 0, 0)

        // 圆形
        // ctx.beginPath()
        // // this.data.supplierName.length > 12 ? 13 : 16 210
        // ctx.arc(160, this.data.supplierName.length > 12 ? 210 : 200, 80, 0, Math.PI * 2, false)
        // ctx.closePath()
        // ctx.fill()
        // ctx.clip()

        // 二维码放置
        ctx.drawImage(this.data.qrcode, 98, this.data.supplierName.length > 12 ? 170 : 160, 140, 140, 0, 0)
        ctx.draw()
        ctx.restore()

        // 字体
        ctx.font =
            'normal normal 20px Monospaced Number,Chinese Quote,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,PingFang SC,Hiragino Sans GB,Microsoft YaHei,Helvetica Neue,Helvetica,Arial,sans-serif'

        // 邀请内容
        // ctx.setTextAlign("left")
        // ctx.setFillStyle('#fff')
        // ctx.fillText(this.data.supplierName, 22, 80)
        // ctx.fillText('邀请您加入', 30 + (user.userName.length * 22), 80)
        // ctx.font =
        // 	'normal normal 24px Monospaced Number,Chinese Quote,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,PingFang SC,Hiragino Sans GB,Microsoft YaHei,Helvetica Neue,Helvetica,Arial,sans-serif'
        // ctx.fillText(this.data.supplierName.substr(0, 12), 22, 120)
        // ctx.fillText(this.data.supplierName.substr(12), 22, 154)
        ctx.textAlign = 'center'
        ctx.font = 'normal bold 13px Arial,san-serif'
        ctx.setFontSize(this.data.supplierName.length > 12 ? 13 : 16)
        ctx.setFillStyle('#202536')
        // 哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈 this.data.supplierName this.data.supplierName
        ctx.fillText(this.data.supplierName.slice(0, 12), 168, 135)
        ctx.fillText(this.data.supplierName.slice(12, 25), 168, 151)
        ctx.fillText(this.data.supplierName.slice(25), 168, 167)
        ctx.setFontSize(12)
        ctx.setFillStyle('#fff')
        // 公司简称
        if (user.companyAbbreviation) {
            // 装饰品
            // 35 275 150 + (6*10)
            // ctx.drawImage('/packagePay/assets/img/poster/ornament-left.png', 155 - (user.companyAbbreviation.length * 13.5), 370, 8, 8, 0, 0)
            // ctx.drawImage('/packagePay/assets/img/poster/ornament-right.png', 155 + (user.companyAbbreviation.length * 13.5), 370, 8, 8, 0, 0)
            // ctx.setFontSize(22)
            // ctx.setFillStyle('#fff')
            // // this.data.supplierName
            // ctx.fillText(user.companyAbbreviation, 160, 380)
        }
        // 公司全称
        ctx.setFontSize(12)
        ctx.setFillStyle('#565868')
        ctx.font = 'normal normal 12px Arial,san-serif'
        ctx.fillText(user.companyName, 170, user.companyAbbreviation ? 110 : 38)




        ctx.draw(true)
        ctx.restore()
        // this.savecanvas()
    },
    savecanvas() {
        wx.canvasToTempFilePath({
            x: 0,
            y: 0,
            width: 350,
            height: 500,
            canvasId: 'myCanvas',
            success: function (res) {
                let img = res.tempFilePath
                wx.saveImageToPhotosAlbum({
                    filePath: img,
                    success() {
                        app.toast('成功保存', 'success')
                    },
                    fail() {
                        app.toast('保存失败')
                    }
                })
            },
            fail(err) {
                console.log(err)
            }
        })
    },
    showMorePosterStyle() {
        this.setData({
            showPosterStyle: true
        })
    }
    // onShareAppMessage(event) {
    // 	console.log('分享', event)
    // 	return {
    // 		title: user.userName + '邀请您加入「' + user.companyName + '」',
    // 		path: '/page/index/index?shareUserId=' + user.userId,
    // 	}
    // }
})